# -*- coding: utf-8 -*-

# Copyright 2013 Ida Leter, adapted from Fanficdownloader
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
#     http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
#

import time
import logging
logger = logging.getLogger(__name__)
import re
import urllib2

from .. import BeautifulSoup as bs
from .. import exceptions as exceptions
from ..htmlcleanup import stripHTML

from base_adapter import BaseSiteAdapter,  makeDate

def getClass():
    return AdsAdapter


logger = logging.getLogger(__name__)

class AdsAdapter(BaseSiteAdapter):

    def __init__(self, config, url):
        BaseSiteAdapter.__init__(self, config, url)

        self.decode = ["utf8","Windows-1252"]
        self.username = "" # if left empty, site doesn't return any message at all.
        self.password = ""
        
        # get paperId
        #http://adsabs.harvard.edu/cgi-bin/nph-data_query?bibcode=2009A%26A...506...85P&link_type=REFERENCES&db_key=AST&high=
        try:
            self.paper.setMetadata('paperId',self.parsedUrl.query.split('bibcode=',)[1])
        except:
            self.paper.setMetadata('paperId',self.parsedUrl.path.split('/',)[2])
        
        # normalized URL.
        self._setURL('http://' + self.getSiteDomain() + '/abs/'+self.paper.getMetadata('paperId'))
        
        # Each adapter needs to have a unique site abbreviation.
        self.paper.setMetadata('siteabbrev','ads')

        # The date format will vary from site to site.
        # http://docs.python.org/library/datetime.html#strftime-strptime-behavior
        self.dateformat = "%m/%Y"
            
    @staticmethod # must be @staticmethod, don't remove it.
    def getSiteDomain():
        # The site domain.  Does have www here, if it uses it.
        return 'adsabs.harvard.edu'

    def getSiteExampleURLs(self):
        return "http://adsabs.harvard.edu/abs/2000xxxx.123.2345A \
             http://adsabs.harvard.edu/cgi-bin/nph-data_query?bibcode=2000xxxx.123.2345A"

    def getSiteURLPattern(self):
        return r"http://adsabs.harvard.edu/(abs|cgi-bin)?/(nph-data_query\?bibcode=)?\w+"

    def extractMetadata(self):
        try:
            data = self._fetchUrl(self.url)
            
        except urllib2.HTTPError, e:
            if e.code == 404:
                raise exceptions.PaperDoesNotExist()
            else:
                raise e
            
        # use BeautifulSoup HTML parser to make everything easier to find.
        soup = bs.BeautifulSoup(data, selfClosingTags=('p','strong'))

        # Now go hunting for all the meta data and the chapter list.
        
        # Title
        a = soup.find('meta', {'name' : 'citation_title'})
        self.paper.setMetadata('title',a['content'])
		
        # Author
        alist = soup.findAll('a', href=re.compile(r"http://adsabs.harvard.edu/cgi-bin/author_form\?author="))
        for a in alist:
            self.paper.addToList('author',unicode(stripHTML(a)))
            self.paper.addToList('authorUrl',a['href'])
            

        # Date
        a = soup.find('meta', {'name' : 'citation_date'})['content']
        try:
            self.paper.setMetadata('datePublished', makeDate(a, self.dateformat))
        except:
        	self.dateformat='%Y'
        	self.paper.setMetadata('datePublished', makeDate(a, self.dateformat))
        
        # Abstract
        def defaultGetattr(d):
            try:
                return d.name
            except:
                return ""
        
        value = soup.find('h3', {'align' : 'center'}).nextSibling
        svalue = ""
        while not defaultGetattr(value) == 'hr':
            svalue += str(value)
            value = value.nextSibling
        self.paper.setMetadata('abstract',stripHTML(svalue))
        
        # Keywords
        alist = soup.find('meta', {'name' : 'citation_keywords'})
        if alist != None:
            for a in alist['content'].split(', '):
                self.paper.addToList('tags',a)
            
        # ArXiv id
        try:
            alist=soup.find('a',href=re.compile("PREPRINT"))
            arxiv=alist.nextSibling.split(':')[1].split(')')[0]
            self.paper.setMetadata('arxiv',arxiv)
            self.paper.setMetadata('pdfurl','http://arxiv.org/pdf/%s.pdf' %self.paper.getMetadata('arxiv'))
        except:
            pass
        # doi
        a = soup.find('meta', {'name' :'citation_doi'})
        if a != None:
            self.paper.setMetadata('doi',a['content'])
            
        
        # Publisher
        a=self.paper.getMetadata('paperId').replace("%26","&")
        publisher=re.split('[.0-9]+',a)[1]
        print publisher
        
        self.paper.setMetadata('publisher',publisher)